 package com.crm.sys.listener;

import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.crm.sys.dao.SysDao;
import com.crm.sys.model.CityAndProvinceDic;
import com.crm.util.Constants;
import com.crm.util.PropertyReader;
@Controller
public class SiteInit implements ServletContextListener{
	private static Logger log = LoggerFactory.getLogger(SiteInit.class);
	
	private ApplicationContext app; 
	
	
	//@Autowired
	//private SysDao sysDao;
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
	}
	
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		try{
		 
			
			
	        app = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()); //获取spring上下文！  
	        SysDao sysDao =  (SysDao)app.getBean("sysDao"); 
	    	
	        Constants.GroupNumber =PropertyReader.loadPropertyParam("data.properties", "group");
	        Constants.GroupName =PropertyReader.loadPropertyParam("data.properties", "groupname");
	   
	        
	        Constants.CALLCENTER.put("callcenter", PropertyReader.loadPropertyParam("data.properties", "callcenter")) ;
	        Constants.CALLCENTER.put("client_id", PropertyReader.loadPropertyParam("data.properties", "client_id")) ;
	        Constants.CALLCENTER.put("client_secret", PropertyReader.loadPropertyParam("data.properties", "client_secret")) ;
	        Constants.CALLCENTER.put("callBak", PropertyReader.loadPropertyParam("data.properties", "callBak")) ;
	        Constants.CALLCENTER.put("instanceId", PropertyReader.loadPropertyParam("data.properties", "instanceId")) ;
	        Constants.CALLCENTER.put("accessKeyId", PropertyReader.loadPropertyParam("data.properties", "accessKeyId")) ;
	        Constants.CALLCENTER.put("accessSecret", PropertyReader.loadPropertyParam("data.properties", "accessSecret")) ;
	        /*   String classPath = Model.class.getResource("/").getPath();
			String jsFilePath = classPath.substring(0, classPath.indexOf("WEB-INF/classes/"))+"javascripts/address1.js";
			Readfile  ss= new Readfile();
		 
	         List<CityList>  map =ss.getRemotCitys(jsFilePath);
			System.out.println(map);
	  
		       for(CityList c:map){
		    	   Province pro = new Province();
		    	   pro.setProvincename(c.getCnName());
		    	   pro.setProvincenubmer(c.getId());
		    	   sysDao.insertProvince(pro);
		    	   
		    	   List<CityList>   sub = c.getSubList();
		    	   for(CityList s:sub){
		    		   City cc= new City();
		    		   cc.setProvince(pro.getId());
		    		   cc.setCityname(s.getCnName());
		    		   cc.setCitynumber(s.getId());;
		    		   sysDao.insertCity(cc);
		    	   }
		    	   
		    	   
		       }*/
			 
	        
	        
	        //加载城市
	        List<CityAndProvinceDic>  list =  sysDao.getCityList();
	        for(CityAndProvinceDic d:list){
	        	Constants.CITYS.put(d.getPid()+"_"+d.getCid(), d);
	        }
	        log.debug(""+list);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}
